package com.xxxx.server.controller;


import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.Authority;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IAuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LT
 * @since 2022-02-27
 */
@Api(value="用户管理",tags = "用户管理")
@RestController
@RequestMapping("")
public class AdminController {

    @Autowired
    private IAdminService adminService;
    @Autowired
    private IAuthorityService authorityService;

    @ApiOperation(value = "通过关键字获取用户")
    @GetMapping("/system/UserinfoManagement/list")
    public List<Admin> getAllUsersByKeywords(String keywords)
    {
        return adminService.getAllUsersByKeywords(keywords);
    }



    @ApiOperation(value = "添加用户")
    @PostMapping("/system/UserinfoManagement/add")

    public RespBean addAdmin(@RequestBody Admin admin)
    {

        if (adminService.save(admin))
        {
            return RespBean.success("添加成功!");
        }
        return RespBean.error("添加失败!");
    }

    @ApiOperation(value = "更新用户信息")
    @PutMapping("/system/LectureInfoManagement/getLectureInfoByPage")
    public RespBean updateAdmin(@RequestBody Admin admin)
    {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        // 加密
        String encodedPassword = passwordEncoder.encode(admin.getPassword().trim());
        admin.setPassword(encodedPassword);
        boolean f=adminService.updateById(admin);

        if(f)
        {
            return RespBean.success("更新成功!");
        }
        return RespBean.error("更新失败!");
    }
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/system/UserinfoManagement/delete")
    public RespBean deleteAdmin(@RequestParam Integer id)
    {
        if(adminService.removeById(id))
        {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败!");
    }

    @ApiOperation(value = "获取所有权限信息")
    @GetMapping("/system/UserinfoManagement/authorityList")
    public List<Authority> getAllAuthority(){
        return authorityService.list();
    }

    @ApiOperation(value = "更新用户权限表")
    @PutMapping("/system/UserinfoManagement/updateAuthority")

    public RespBean updateAdminAuthority(@RequestParam("userId") Integer userId, @RequestParam("authorityIds") List<Integer> authorityIds)
    {
        return adminService.addAdminAuthority(userId,authorityIds);
    }


}
